<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Pagination\LengthAwarePaginator;

class MaicheController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function index(Request $request ,$cate_id = 0,$chexi_id = 0,$pfw = 0,$age = 0,$car_type = -1,$licheng = 0,$biansu = 0,$pailiang = 0,$biaozhun = 0,$paixu = 0)
    {
        //页面栏目初始信息数据
        $cateData = DB::table('cates')->where('parent_id',-1)->take(20)->get();
        $cateDatas = DB::table('cates')->where('parent_id',-1)->get();
        if($cate_id == 0){
            $chexi = DB::table('cates')->where('parent_id','!=',-1)->take(20)->get();
        }else{
            $chexi = DB::table('cates')->where('parent_id',$cate_id)->get();
        }
       
       // 价格范围信息
        $pricefw = DB::table('pricefanweis')->get();  
    
        //页面车信息初始数据
        $car_info = $this->getData($cate_id,$chexi_id,$pfw,$age,$car_type,$licheng,$biansu,$pailiang,$biaozhun,$paixu);

        //手动分页开始部分
        $page = 1;
        if($request->input('page'))
        {
            $page = $request->input('page');
        }
        $pagesize = 12;
        $pageout = array_slice($car_info, ($page-1)*$pagesize,$pagesize);
        $total = count($car_info);
        $paged = new LengthAwarePaginator($pageout,$total,$pagesize);
        //这里原来是$paged->setPath("maiche")但是发现他会在地址栏的maiche后面追加page=1这些参数就会导致出问题因此这样处理
        $paged->setPath("");

        $paixu = ($paixu == 0)? 1 : 0;
        // 给模板传参
        return view('home.buycar',
            [
            //车源栏目,车系,车源列表
            'cateData'=>$cateData,
            'cateDatas'=>$cateDatas,
            'chexi'=>$chexi,
            'car_info'=>$paged,
            //车辆数,和给模板传参用于判断是否被选中
            'num'=>$total,
            'u'=>$cate_id,
            'chexi_id'=>$chexi_id,
            'pricefw'=>$pricefw,
            'pid'=>$pfw,
            'age'=>$age,
            'car_type'=>$car_type,
            'licheng'=>$licheng,
            'biansu'=>$biansu,
            'pailiang'=>$pailiang,
            'guobie'=>$biaozhun,
            'paixu'=>$paixu
            ]);

    }


    //根据各种条件去获取车源数据

    public function getData($cate_id,$chexi_id,$pfw,$age,$car_type,$licheng,$biansu,$pailiang,$biaozhun,$paixu){
        $row = [];
        if($paixu == 0){
            $res = DB::table('car_infos')->orderBy('car_price','asc');
        }else{
            $res = DB::table('car_infos')->orderBy('car_price','desc');
        }
        $yz = DB::table('cates');

        //当页面什么参数都没返回页面初始的信息
        if($cate_id == 0 && $chexi_id == 0 && $pfw == 0 && $age == 0 && $car_type == -1 && $licheng == 0 && $biansu == 0 && $pailiang == 0 && $biaozhun == 0){
            return $res->get();
        }
        //如果首页传来车型的值时返回的页面信息
        if($cate_id == 0 && $chexi_id == 0 &&$pfw == 0 && $age == 0 && $car_type != -1 && $licheng == 0 && $biansu == 0 && $pailiang == 0 && $biaozhun == 0){
            if($car_type == 5){
                return $res->whereIn('car_type',[3,4])->get();
            }else{
                return $res->where('car_type',$car_type)->get();
            } 
        }

        //准备好如果传来的值cate_id是品牌的ID 就循环查出它下面的车系cate_id作为车源表的查询条件
        if($cate_id != 0){
            $cate = $yz->where('cate_id',$cate_id)->first();
            if($cate->parent_id == -1){
                $chexi = DB::table('cates')->where('parent_id',$cate_id)->get();
                foreach($chexi as $c){
                    $row[] = $c->cate_id;
                }
            }else{
                $row[] = $cate_id;
            }
            $res = $res->whereIn('cate_id',$row);
        }

        if($chexi_id != 0){
            $res = $res->where('cate_id',$chexi_id);
        }

        if($pfw != 0){
            $res = $res->where('pricefanwei',$pfw);

        }

        if($age == 6){
            $res = $res->where('car_age','>=',$age);
        }elseif($age > 0){
            $res = $res->where('car_age','<=',$age);
        }

        if($car_type == 5){
            $res = $res->whereIn('car_type',[3,4]);
        }elseif($car_type >=0){
            $res = $res->where('car_type',$car_type);
        }

        if($licheng != 0 ){
            if($licheng == 6){
                $res = $res->where('car_licheng','>=',$licheng);
            }else{
                $res = $res->where('car_licheng','<=',$licheng);
            }
        }

        if($biansu != 0){
            if($biansu == 1){
                $res = $res->where('car_biansu','手动');
            }else{
                $res = $res->where('car_biansu','自动');
            }
        }

        if($pailiang != 0){
            if($pailiang == 1){
                $res = $res->where('car_pailiang','1.0T');
            }elseif($pailiang == 2){
                $res = $res->where('car_pailiang','2.0T');
            }elseif($pailiang == 3){
                $res = $res->where('car_pailiang','3.0T');
            }else{
                $res = $res->where('car_pailiang','4.0T');
            }
        }

        if($biaozhun != 0){
            if($biaozhun == 1){
                $res = $res->where('car_pfbz','国二');
            }elseif($biaozhun == 2){
                $res = $res->where('car_pfbz','国三');
            }elseif($biaozhun == 3){
                $res = $res->where('car_pfbz','国四');
            }else{
                $res = $res->where('car_pfbz','国五');
            }
        }
        return $res->get();

   
    }

    public function mhcx($cate_id = 0 , $cate_name = ''){
        if($cate_name != ''){
            $res = DB::table('cates')->where('cate_name',$cate_name)->first();
            if($res){
                return redirect("maiche/$res->cate_id");
            }else{
                return back()->withInput();;
            }
            
        }else{
            $cate_names['a'] = '奥迪 阿斯顿马丁';
            $cate_names['b'] = '宝马 别克 本田 奔驰 比亚迪';
            $cate_names['c'] = '长城 长安 昌河 长丰';
            $cate_names['d'] = '大众 道奇 东风 大宇';
            $cate_names['f'] = '法拉利 福特 丰田 菲亚特';
            $cate_names['h'] = '哈飞 海马 悍马 红旗';
            $cate_names['j'] = '吉利 吉普 江铃 江淮 捷豹';
            $cate_names['k'] = '克莱斯勒 凯迪拉克 开瑞';
            $cate_names['l'] = '劳斯莱斯 雷克萨斯 林肯 路虎 兰博基尼 陆风 铃木';
            $cate_names['m'] = '马自达 名爵 玛莎拉蒂 迈巴赫 迷你';
            $cate_names['n'] = '尼桑';
            $cate_names['o'] = '讴歌 欧宝';
            $cate_names['q'] = '起亚 奇瑞';
            $cate_names['r'] = '荣威 瑞麒';
            $cate_names['s'] = '三菱 萨博 斯巴鲁 斯柯达 曙光 双环 双龙 上海英伦';
            $cate_names['t'] = '天津一汽';
            $cate_names['w'] = '五菱 沃尔沃 万丰 威麟 五十铃';
            $cate_names['x'] = '现代 雪铁龙 雪佛兰 雪佛兰';
            $cate_names['y'] = '野马 一汽 跃进 依维柯 宇通 英菲尼迪';
            $cate_names['z'] = '中华 中顺 众泰 中兴';
            if(!empty($cate_id)){
                return isset($cate_names[$cate_id]) ? $cate_names[$cate_id] : 0;
            }else{
                return 1;
            }
            
        }
    }
}
